package it.hive.controller;

import it.hive.controller.listener.VerificatoreProxy;
import it.hive.controller.listener.VerificatoreProxy.Check;
import it.hive.model.Application;
import it.hive.model.common.ApplicationProperties;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.DirectoryChooser;

public class SettingsController implements Initializable {
	@FXML
	private Pane proxyPanel;
	@FXML
	private TextField proxyIP;
	@FXML
	private TextField proxyPort;
	@FXML
	private TextField defaultDownloadDirectory;
	@FXML
	private CheckBox proxyCheckBox;
	@FXML
	private CheckBox scaricaAutomaticoInDownloadDirectory;
	@FXML
	private CheckBox abilitaAnimazioniAudio;
	@FXML
	private Button buttonSalva;
	@FXML
	private Slider livelloAnimazione;

	private Application application;

	private SimpleBooleanProperty impostazioniSalvate;

	private VerificatoreProxy ip;
	private VerificatoreProxy port;

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		proxyPanel.disableProperty().bind(proxyCheckBox.selectedProperty().not());
		application = Application.getIstanza();

		defaultDownloadDirectory.textProperty().bindBidirectional(ApplicationProperties.defaultDownloadDirectoryPath);
		scaricaAutomaticoInDownloadDirectory.selectedProperty().bindBidirectional(ApplicationProperties.boolUsaSempreDefaultDownloadDirectory);

		proxyCheckBox.selectedProperty().bindBidirectional(ApplicationProperties.boolUsaProxy);

		ip = new VerificatoreProxy(Check.IP_ADDRESS, proxyIP.textProperty());
		port = new VerificatoreProxy(Check.PORT, proxyPort.textProperty());

		proxyIP.textProperty().bindBidirectional(ApplicationProperties.proxyIP);
		proxyPort.textProperty().bindBidirectional(ApplicationProperties.proxyPort);

		proxyIP.backgroundProperty().bind(ip.getBackgroundProperty());
		proxyPort.backgroundProperty().bind(port.getBackgroundProperty());

		proxyIP.focusedProperty().addListener(ip);
		proxyPort.focusedProperty().addListener(port);

		impostazioniSalvate = new SimpleBooleanProperty(false);
		buttonSalva.disableProperty().bind(
				(proxyCheckBox.selectedProperty().and(ip.valoreIpCorretto().and(port.valorePortaCorretto()).not())).or(impostazioniSalvate));
		abilitaAnimazioniAudio.selectedProperty().bindBidirectional(ApplicationProperties.animazioneAudioAbilitata);
		livelloAnimazione.setMin(0);
		livelloAnimazione.setMax(1);
		livelloAnimazione.setValue(0.5);
		ApplicationProperties.livelloAnimazioneAudio.bind(livelloAnimazione.valueProperty());
	}

	@FXML
	public void selezionaDirectoryDownload(MouseEvent mouse) {
		DirectoryChooser directoryChooser = new DirectoryChooser();
		directoryChooser.setTitle("Seleziona la directory dove scaricare i files");
		directoryChooser.setInitialDirectory(new File(System.getProperty("user.home")));
		File f = directoryChooser.showDialog(null);
		if (f != null) {
			String path = f.getAbsolutePath();
			defaultDownloadDirectory.setText(path);
		}
	}

	@FXML
	public void verificaImpostazioniProxy() {
		System.out.println("verifica impostazioni proxy");
	}

	@FXML
	public void salvaImpostazioni() {
		if (!proxyCheckBox.isSelected()) {
			impostazioniSalvate.set(application.salvaImpostazioniProxy("", ""));
			return;
		}
		if (ip.valoreIpCorretto().get() && port.valorePortaCorretto().get()) {
			if (proxyCheckBox.isSelected()) {
				impostazioniSalvate.set(application.salvaImpostazioniProxy(proxyIP.textProperty().get(), proxyPort.textProperty().get()));
			} else {
				impostazioniSalvate.set(application.salvaImpostazioniProxy("", ""));
			}
		}

	}

}
